$(function () {

    window.startDate = $('#datePicker').find('.start').html();  //开始时间
    window.endDate = $('#datePicker').find('.end').html();      //结束时间
    window.creator = '0';    //操作人
    window.roomtype = '0';   //房型
    window.pricetype = '0';   //状态
    window.sale = '0';     //操作时间
    var loading = $('#loading');
    var toggleLoading = function (isShow) {
        if (isShow) {
            loading.removeClass('hidden');
        } else {
            loading.addClass('hidden');
        }
    };

    var tip = function (obj, info) {
        obj.tip({
            content: info,
            background: "#000",
            color: "#fff"
        });
    };
    
    var changeTypeConstant = [lang('allPrice'), lang('defaultPrice'), lang('salesroomPrice'), lang('bookPrice'), lang('endPrice'), lang('hourPrice'), lang('longPrice'), lang('charity')];
    var refreshLayout = function (datas) {
        var reportInfos = datas.item;
        if (reportInfos.length === 0) {
            $('#changeListBody').html('');
            $('#changeListBodyEmpty').removeClass('hidden');
            $('#changeListPage').addClass('hidden');
        } else {
            $('#changeListBodyEmpty').addClass('hidden');
            var templateHTML = $($('#templateChange').clone().html());
            var reportsHTML = '';
            $.each(reportInfos, function (key, reportInfo) {
                templateHTML.find('.price-creattime').html(reportInfo.createtime);
                templateHTML.find('.price-operator').html(window.creatorConstant[reportInfo.creator]);
                templateHTML.find('.price-change-type').html(changeTypeConstant[reportInfo.pricetype]);
                templateHTML.find('.price-room-type').html(window.roomtypeConstant[reportInfo.roomtype_id]);
                if(reportInfo.pricetype == "4"){
                    reportInfo.startdate += lang('to') +  reportInfo.enddate;
                }
                templateHTML.find('.price-sale-time').html(reportInfo.startdate);
                templateHTML.find('.price-detail').html(reportInfo.detail);
                reportsHTML += templateHTML.prop('outerHTML');
            });
            $('#changeListBody').html(reportsHTML);
            var pageInfo = datas.page;
            var currentPage = parseInt(pageInfo.currentPage);
            var pages = parseInt(pageInfo.total);
            var pageContainer = $('#changeListPage');
            pageContainer.removeClass('hidden');
            var pageHTML = '';
            var templatePage = $($('#templatePage').clone().html());
            for (var i = 1; i <= pages; i++) {
                if (currentPage === i) {
                    templatePage.addClass('active');
                } else {
                    templatePage.removeClass('active');
                }
                templatePage.find('a').html(i).attr('data-value', i);
                pageHTML += templatePage.prop('outerHTML');
            }
            pageContainer.find('.pagination').html(pageHTML);
            pageContainer.find('.page-count').html(pages);
            pageContainer.find('.page-total').html(pageInfo.totalCount);
        }
    };
    var requestInfo = function (page, submitBtn) {
        toggleLoading(true);
        var params = {
            'hotel': window.currentHotel,
            'currentPage': page,
            'startDate': window.startDate,
            'endDate': window.endDate,
            'roomtype': window.roomtype,
            'creator': window.creator,
            'pricetype': window.pricetype,
            'sale': window.sale,
        };
        ajaxRequest('/Home/Order/changeReport', params, 'post', function (data) {
            toggleLoading(false);
            window.serverDatas = data.data;
            refreshLayout(window.serverDatas);
        }, function (error, obj) {
            toggleLoading(false);
            if (typeof error.responseJSON == 'undefined') {
                tip(obj, lang('tip.system'));
                return;
            }

            tip(obj, error.responseJSON.info);
        }, submitBtn);
    };

    window.creatorConstant = [];  //构造数组类型
    window.roomtypeConstant = []; //构造数组类型
    //头部赋值
    ajaxRequest('/Home/Order/changeReportTitle', {'hotel': window.currentHotel}, 'post', function (data) {
            var data = data.data;
            var creatorHTML = '';
            $.each(data.creator, function (key, creator) {
                window.creatorConstant[creator.creator] = creator.name;
                var templateHTML = $($('#templatePage').clone().html());
                templateHTML.find('a').html(creator.name).attr('data-name','creator').attr('data-id',creator.creator);
                creatorHTML += templateHTML.prop('outerHTML');
            });
            $('#priceCreator').html(creatorHTML);
            var roomtypeHTML = '';
            $.each(data.roomType, function (key, roomType) {
                window.roomtypeConstant[roomType.roomtype_id] = roomType.name;
                var templateHTML = $($('#templatePage').clone().html());
                if(roomType.isdelete === '1'){
                    roomType.name = roomType.name+'<span class="text-accent">('+lang("alreadyDeleted")+')</span>';
                }
                templateHTML.find('a').html(roomType.name).attr('data-name','roomtype').attr('data-id',roomType.roomtype_id);
                roomtypeHTML += templateHTML.prop('outerHTML');
            });
            $('#filterRoomMenu').html(roomtypeHTML);

            requestInfo(1, $('#filterSearch'));

        },null);

    $('#changeListPage .pagination').on('click', 'li:not(.active) a', function () {
        var pageIndex = $(this).data('value');
        var submitBtn = $('#filterSearch');
        requestInfo(pageIndex, submitBtn);
    });

    $('#filterHeader').on('click', '.dropdown-menu li>a', function (e) {
        var item = $(this);
        var liItem = item.parents('li');
        var toggle = liItem.parents('.dropdown-menu').siblings('[data-toggle="dropdown"]');
        liItem.addClass('active');
        liItem.siblings().removeClass('active');
        var text = item.text();
        toggle.find('.info').html(text);
        var name = item.data('name');
        var id = item.data('id');
        window[name] = id;
    });
    // 选择日期
    var format = 'YYYY-MM-DD';
    $('#datePickerInput').daterangepicker({
        'showDropdowns': true,
        'autoApply': true,
        'locale': {
            'format': format,
            'applyLabel': lang('date.ensure'),
            'cancelLabel': lang('date.cancel'),
            'daysOfWeek': [
                lang('week.7'),
                lang('week.1'),
                lang('week.2'),
                lang('week.3'),
                lang('week.4'),
                lang('week.5'),
                lang('week.6')
            ],
            'monthNames': [
                lang('month.1'),
                lang('month.2'),
                lang('month.3'),
                lang('month.4'),
                lang('month.5'),
                lang('month.6'),
                lang('month.7'),
                lang('month.8'),
                lang('month.9'),
                lang('month.10'),
                lang('month.11'),
                lang('month.12')
            ],
            'firstDay': 1
        },
        'linkedCalendars': false,
        'parentEl': 'body',
        'startDate': window.startDate,
        'endDate': window.endDate,
        'minDate': '2017-03-01',
        'maxDate': $('#doc').data('year')
    }, function (start, end, label) {
        window.startDate = start.format(format);
        window.endDate = end.format(format);
        $('#datePicker').find('.start').html(window.startDate);
        $('#datePicker').find('.end').html(window.endDate);
    });
    $('#datePicker').on('click', function () {
        $('#datePickerInput').trigger('focus');
    });

    // 查询筛选
    $('#filterSearch').on('click', function () {
        var submitBtn = $(this);
        requestInfo(1, submitBtn);
    });
});
